#!/bin/bash


cur_day=$(date +%Y-%m-%d)
pre_day=$(date -d'-1 day' +%Y-%m-%d)


# 添加 pre_day 的采集结果，到临时表的分区
beeline -u jdbc:hive2://doitedu:10000 -n root -e "alter table tmp.user_action_log add partition(dt='${pre_day}') location '/events_log/${pre_day}'"

# 执行sql来统计表中的行数
x=$(beeline -u jdbc:hive2://doitedu:10000 -n root --outputformat=csv2 -e "select count(1) as cnt from tmp.user_action_log where dt='${pre_day}';")
line_cnt=$(echo $x | cut -d' ' -f 2)

# 拼接结果json
# {"table_name":"tmp.user_action_log","target_dt":"2024-04-17","run_dt":"2024-04-18","line_cnt":8}
kv1='"table_name":"tmp.user_action_log"'
kv2='"target_dt":"'$pre_day'"'
kv3='"run_dt":"'$cur_day'"'
kv4='"line_cnt":'$line_cnt

res_json="{$kv1,$kv2,$kv3,$kv4}"

echo $res_json

# 上报到数据平台(质量管理系统)
res=$(curl -X POST -H "Content-Type: application/json" -H "Accept:application/json" -d "$res_json" http://192.168.77.2:8080/dq/log_hdfs)
if [ $res == "success" ]
then
  echo "上报成功"
else
  echo "上报失败"
fi